<?php

namespace app\admin\controller;


use think\Db;
use think\mongo\Connection;
use my\Security;
use my\Tools;
class Test extends Base {
    //用户列表
    public function index(){
        $data=Db::name('driver_base a,xm_driver_info b')->field('a.driver_id,a.driver_phone,a.driver_name,a.driver_type,b.work_province,b.work_city')->where("a.driver_name!='' and a.driver_id=b.driver_id")->select();

        $arr=array();
        $distanceMax=20000;//距离（米）
        //117.281553,31.859653
        $lat=31.82057;
        $lng=117.227308;
        foreach($data as $k=>$v){
            $strArr=array();
            $strArr['driver_id']=$v['driver_id'];
            $strArr['driver_phone']=Security::decrypt($v['driver_phone']);
            $strArr['driver_name']=$v['driver_name'];
            $strArr['work_province']=$v['work_province'];
            $strArr['work_city']=$v['work_city'];
            $strArr['driver_type']=$v['driver_type'];

            $jd=rand(0,359);
            $distance=rand(1,$distanceMax);
            $latlng=$this->computationx($lat,$lng, $jd,$distance);
            $strArr['lng']=$latlng['lng'];
            $strArr['lat']=$latlng['lat'];
            $strArr['lng_old']=$latlng['lng'];
            $strArr['lat_old']=$latlng['lat'];
            $strArr['direction']=$jd;//方位角
            $strArr['randNum_s']=rand(1800,86400);//生成时得到的随机数，在线时长(秒)
            $strArr['randNum_e']=rand(600,28800);//生成时得到的随机数，不在线时长(秒)
            $strArr['time_old']=time();//上次更新时间

            $strArr['randNum_s_c']=$strArr['randNum_s'];//在线时长(秒)
            $strArr['randNum_e_c']=0;//不在线时长(秒)
            $strArr['time_new']=time();//最新更新时间

            $strArr['ddNum']=0;//订单数

            if(rand(0,10)>5){
                $s1=rand(1,10);
                $s2=rand($s1,$s1*2);
                $strArr['ddNum']=rand($s1,$s2);//订单数
            }

            Db::name('driver_location')->insertGetId($strArr);
            $arr[]=$strArr;
        }

        echo "<pre>";
        print_r($arr);
        exit;
        $arr=array();
        for($i=0;$i<100;$i++){
            $strArr=array();
            $name=$this->generateName();
            $tel=$this->SJtel();
            $str=$name.'/'.$tel;
            $strArr[0]=$str;

            $jd=rand(0,359);
            $distance=rand(1,20000);
            $sss=$this->computationx(31.82057,117.227308, $jd,$distance);
            $strArr[1]=$sss['lng'];
            $strArr[2]=$sss['lat'];
            $arr[]=$strArr;
        }
        $config['arr']=json_encode($arr);

        $this->assign($config);
        return view();
    }
    /**
     * @desc 根据两点间的经纬度查找范围内司机
     * @param float $lat 起点纬度值
     * @param float $lng 起点经度值
     * @param float $lng_e 终点纬度值
     * @param float $lat_e 终点经度值
     * @param float $distance 默认半径1km
     */
    public function driverLocation(){
        $data=$_GET;

        $lng=$data['lng']>0?$data['lng']:117.323554;
        $lat=$data['lat']>0?$data['lat']:31.877841;

        $lng_e=$data['lng_e']>0?$data['lng_e']:117.323543;
        $lat_e=$data['lat_e']>0?$data['lat_e']:31.879404;
        $distance=1;
        return self::getDriverLocation($lng,$lat,$lng_e,$lat_e,$distance);
    }
    /**
     * @desc 根据两点间的经纬度查找范围内司机
     * @param float $getLng 起点纬度值
     * @param float $getLat 起点经度值
     * @param float $getLng_e 终点纬度值
     * @param float $getLat_e 终点经度值
     * @param float $getDistanceX 默认半径1km,1km没找到就加1,直至5km
     */
    public function getDriverLocation($getLng,$getLat,$getLng_e,$getLat_e,$getDistanceX)
    {
        $lng=$getLng;
        $lat=$getLat;

        $lng_e=$getLng_e;
        $lat_e=$getLat_e;
        //获取边长（$distance*2）公里的正方形四个点的坐标
        $distance=$getDistanceX;
        $SquarePoint=self::returnSquarePoint($lng,$lat,$distance);

        //获取乘客方向角
        $Angle=self::computeAzimuth($lat,$lng,$lat_e,$lng_e);


        //$mongoCollection    = new Connection(config('mongodb'));
        $mongoCollection    = new Connection();
        $driverLocation     = $mongoCollection->table("live_driver_location")
            ->field('driverId,location,createTime,direction')
            ->where('location.coordinates.0','>',$SquarePoint['left_top']['lng'])
            ->where('location.coordinates.0','<',$SquarePoint['right_top']['lng'])
            ->where('location.coordinates.0','>',$SquarePoint['left_bottom']['lng'])
            ->where('location.coordinates.0','<',$SquarePoint['right_bottom']['lng'])

            ->where('location.coordinates.1','<',$SquarePoint['left_top']['lat'])
            ->where('location.coordinates.1','<',$SquarePoint['right_top']['lat'])
            ->where('location.coordinates.1','>',$SquarePoint['left_bottom']['lat'])
            ->where('location.coordinates.1','>',$SquarePoint['right_bottom']['lat'])
            ->select();
        if(count($driverLocation)==0){
            return $this->getDriverLocation($getLng,$getLat,$getLng_e,$getLat_e,$getDistanceX);
        }
        //获取乘客（$distance）范围内圆的半径（单位是米）
        $Distance=self::getDistance($lat,$lng,$lat,$SquarePoint['left_top']['lng']);

        $arr=array();
        $dqtime=strtotime(date('Y-m-d'));//当前时间戳
        foreach($driverLocation as $k=>$v){
            //获取乘客与司机间的距离（单位是米）
            $getDistance=self::getDistanceGE($lat,$lng,$v['location']['coordinates'][1],$v['location']['coordinates'][0]);

            //截取时间戳（$v['createTime']这个到毫秒，截取到秒）
            $createTime = substr($v['createTime'], 0, -3);
            if(strstr($v['createTime'],':')){
                $createTime = strtotime($v['createTime']);
            }
            //留下半径内的司机
            if($Distance>=$getDistance&&$createTime>$dqtime){
                //获取乘客与司机的方向角
                $DriverAngle=self::computeAzimuth($lat,$lng,$v['location']['coordinates'][1],$v['location']['coordinates'][0]);
                $strDatas=array();
                $strDatas['userid']=$v['driverId'];
                $strDatas['location']=implode(",",$v['location']['coordinates']);
                $strDatas['updatetime']=$createTime;
                $strDatas['distance']=(int)$getDistance;
                $strDatas['direction']=(int)$v['direction'];
                $strDatas['driver_angle']=$DriverAngle;
                $arr[]=$strDatas;
            }
        }
        $newData = $this->sortArr($arr,'distance',SORT_ASC,SORT_NUMERIC);
        foreach($newData as $kk=>$vv){
            $newData[$kk]['sort1']=$kk+1;
        }

        $azimuth4=self::azimuth4($Angle);
        /*echo "<pre>";
        print_r($azimuth4);*/
        foreach($newData as $kkk=>$vvv){
            $newData[$kkk]['sort2']=8;
            if($azimuth4[1][0]<$azimuth4[1][2]){
                if($azimuth4[1][0]<=$vvv['driver_angle']&&$vvv['driver_angle']<=$azimuth4[1][2]){
                    $newData[$kkk]['sort2']=self::sort2($azimuth4,$vvv,1);
                    continue;
                }
            }else{
                if($azimuth4[1][0]<=$vvv['driver_angle']||$vvv['driver_angle']<$azimuth4[1][2]){
                    $newData[$kkk]['sort2']=self::sort2($azimuth4,$vvv,1);
                    continue;
                }
            }
            if($azimuth4[2][0]<$azimuth4[2][2]){
                if($azimuth4[2][0]<=$vvv['driver_angle']&&$vvv['driver_angle']<=$azimuth4[2][2]){
                    $newData[$kkk]['sort2']=self::sort2($azimuth4,$vvv,2);
                    continue;
                }
            }else{
                if($azimuth4[1][0]<=$vvv['driver_angle']||$vvv['driver_angle']<$azimuth4[1][2]){
                    $newData[$kkk]['sort2']=self::sort2($azimuth4,$vvv,2);
                    continue;
                }
            }
            if($azimuth4[3][0]<$azimuth4[3][2]){
                if($azimuth4[3][0]<=$vvv['driver_angle']&&$vvv['driver_angle']<=$azimuth4[3][2]){
                    $newData[$kkk]['sort2']=self::sort2($azimuth4,$vvv,3);
                    continue;
                }
            }else{
                if($azimuth4[3][0]<=$vvv['driver_angle']||$vvv['driver_angle ']<$azimuth4[3][2]){
                    $newData[$kkk]['sort2']=self::sort2($azimuth4,$vvv,3);
                    continue;
                }
            }
        }
        /*echo "<pre>";
        print_r($newData);*/

        foreach($newData as $key=>$val){
            $ss=($val['sort1']+$val['sort2'])/2;
            $newData[$key]['sort3']=round($ss,2);
        }
        $newData = $this->sortArr($newData,'sort3',SORT_ASC,SORT_NUMERIC);

        $getDistanceX=$getDistanceX+1;
        //
        if($getDistanceX<6){
            if(count($newData)>0){
                echo json_encode(array('status' => '1', 'info' => 'OK','infocode' => '10000','distance' => ($getDistanceX-1),'direction'=>$Angle,'count' => count($newData),'datas'=>$newData));
                exit();
            }else{
                return $this->getDriverLocation($getLng,$getLat,$getLng_e,$getLat_e,$getDistanceX);
            }
        }else{
            echo json_encode(array('status' => '1', 'info' => 'OK','infocode' => '10000','distance' => ($getDistanceX-1),'direction'=>$Angle,'count' => count($newData),'datas'=>$newData));
            exit();
        }
    }

    //driver_angle 司机相对乘客的方位，direction司机的行驶的方向
    function sort2($azimuth4,$vvv,$sun){
        $str=$sun+1;
        if($azimuth4[$sun][0]<=$vvv['driver_angle']&&$vvv['driver_angle']<=$azimuth4[$sun][1]){
            if($azimuth4[4][0]<$azimuth4[4][2]){
                if($azimuth4[4][0]<=$vvv['driver_angle']&&$vvv['driver_angle']<=$azimuth4[4][1]){
                    $sum=$azimuth4[4][1]-$vvv['direction'];
                    if($sum<=45&&$sum>=0){
                        $str=$sun;
                    }
                }
            }else{
                if($azimuth4[4][0]<=$vvv['driver_angle']||$vvv['driver_angle']<=$azimuth4[4][1]){
                    $sumes=$azimuth4[4][1]-$vvv['direction'];
                    if($sumes<0){
                        $sumes=360+$sumes;
                        if($sumes<=45){
                            $str=$sun;
                        }
                    }else{
                        if($sumes<=45){
                            $str=$sun;
                        }
                    }
                }
            }
        }else{
            if($azimuth4[4][0]<$azimuth4[4][2]){
                if($azimuth4[4][1]<=$vvv['driver_angle']&&$vvv['driver_angle']<=$azimuth4[4][2]){
                    $direction=$vvv['direction']==0?360:$vvv['direction'];
                    $sumes=$direction-$azimuth4[4][1];
                    if($sumes<0){
                        $sumes=360+$sumes;
                        if($sumes<=45){
                            $str=$sun;
                        }
                    }else{
                        if($sumes<=45){
                            $str=$sun;
                        }
                    }
                }
            }else{
                if($azimuth4[4][1]<=$vvv['direction']||$vvv['direction']<$azimuth4[4][2]){
                    $direction=$vvv['direction']==0?360:$vvv['direction'];
                    $sumes=$direction-$azimuth4[4][1];
                    if($sumes<0){
                        $sumes=360+$sumes;
                        if($sumes<=45){
                            $str=$sun;
                        }
                    }else{
                        if($sumes<=45){
                            $str=$sun;
                        }
                    }
                }
            }
        }
        return $str;
    }
    function sort2_2($azimuth4,$vvv,$sun){
        if($azimuth4[$sun][0]<=$vvv['driver_angle']&&$vvv['driver_angle']<=$azimuth4[$sun][1]){
            if($azimuth4[4][0]<$azimuth4[4][2]){
                if($azimuth4[4][0]<=$vvv['direction']&&$vvv['direction']<=$azimuth4[4][1]){
                    return $sun;
                }else{
                    return $sun+1;
                }
            }else{
                if($azimuth4[4][0]<=$vvv['direction']||$vvv['direction']<$azimuth4[4][1]){
                    return $sun;
                }else{
                    return $sun+1;
                }
            }
        }else{
            if($azimuth4[4][0]<$azimuth4[4][2]){
                if($azimuth4[4][1]<=$vvv['direction']&&$vvv['direction']<=$azimuth4[4][2]){
                    return $sun;
                }else{
                    return $sun+1;
                }
            }else{
                if($azimuth4[4][1]<=$vvv['direction']||$vvv['direction']<$azimuth4[4][2]){
                    return $sun;
                }else{
                    return $sun+1;
                }
            }
        }
    }

    /*      116     117     118
     * ------+------+-------+------32
     *       |      |       |
     * ------+------+-------+------31
     *       |      |       |
     * ------+------+-------+------30
     * */
    private function returnSquarePoint($lng, $lat,$distance = 3)
    {
        $earthdata=6371;//地球半径，平均半径为6371km 6371.393
        //asin反正弦  sin正弦  cos余弦  rad2deg把角度值转换为弧度值
        $dlng =  2 * asin(sin($distance / (2 * $earthdata)) / cos(deg2rad($lat)));
        $dlng = rad2deg($dlng);
        $dlat = $distance/$earthdata;
        $dlat = rad2deg($dlat);
        $arr=array(
            'left_top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
            'right_top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
            'left_bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
            'right_bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
        );
        return $arr;
    }

    /**
     * @desc 根据两点间的经纬度计算距离
     * @param float $lat 纬度值
     * @param float $lng 经度值
     */
    function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6371000;//6367000
        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;
        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;
        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;
        return round($calculatedDistance);
    }

    /**
     * @desc 高德根据两点间的经纬度计算距离
     * @param float $lat 纬度值
     * @param float $lng 经度值
     */
    function test111(){
        $lng=117.323554;
        $lat=31.877841;

        $lng_e=117.323543;
        $lat_e=31.879404;
        self::getDistanceGE($lat,$lng,$lat_e,$lng_e);
    }
    function getDistanceGE($lat_e, $lng_e, $lat, $lng)
    {
        return self::getDistance($lat, $lng, $lat_e, $lng_e);

        $key1="44f4b1c6a926038c91fc78499dc7686c";
        $origin=$lng.','.$lat;
        $destination=$lng_e.','.$lat_e;
        $url="https://restapi.amap.com/v3/direction/driving?origin=".$origin."&destination=".$destination."&extensions=all&key=".$key1;

        $file_Data = self::file_get_content($url);

        $file_Data=json_decode($file_Data,true);

        if($file_Data['status']=="1"&&$file_Data['infocode']=="10000"){
            return round($file_Data["route"]["paths"][0]["distance"]);
        }else{
            return self::getDistance($lat, $lng, $lat_e, $lng_e);
        }
    }

//传入两个经纬度坐标，计算出两个坐标之间的角度，范围是0到<360。
// 计算方位角,正北向为0度，以顺时针方向递增
    function computeAzimuth($lat1, $lng1, $lat2,$lng2) {
        $result = 0.0;
        $ilat1 = (0.50 + $lat1 * 360000.0);
        $ilat2 = (0.50 + $lat2 * 360000.0);
        $ilon1 = (0.50 + $lng1 * 360000.0);
        $ilon2 = (0.50 + $lng2 * 360000.0);

        $lat1 = deg2rad($lat1);
        $lon1 = deg2rad($lng1);
        $lat2 = deg2rad($lat2);
        $lon2 = deg2rad($lng2);

        if (($ilat1 == $ilat2) && ($ilon1 == $ilon2)) {
            return $result;
        } else if ($ilon1 == $ilon2) {
            if ($ilat1 > $ilat2)
                $result = 180.0;
        } else {
            $c = acos(sin($lat2)*sin($lat1)+cos($lat2)*cos($lat1)*cos(($lon2 - $lon1)));
            $A = asin(cos($lat2) * sin(($lon2 - $lon1))/sin($c));
            $result = rad2deg($A);
            if (($ilat2 > $ilat1) && ($ilon2 > $ilon1)) {
            } else if (($ilat2 < $ilat1) && ($ilon2 < $ilon1)) {
                $result = 180.0 - $result;
            } else if (($ilat2 < $ilat1) && ($ilon2 > $ilon1)) {
                $result = 180.0 - $result;
            } else if (($ilat2 > $ilat1) && ($ilon2 < $ilon1)) {
                $result += 360.0;
            }
        }
        return round($result,1);
    }


    /**
     * @desc 根据两点间的经纬度计算方向角
     * @param float $lat1 起点纬度值
     * @param float $lng1 起点经度值
     * @param float $lat2 终点纬度值
     * @param float $lng2 终点经度值
     */
    function getAngle($lat1, $lng1, $lat2,$lng2)
    {
        $x1 = $lng1;
        $y1 = $lat1;
        $x2 = $lng2;
        $y2 = $lat2;
        $pi = M_PI;
        $w1 = $y1 / 180 * $pi;
        $j1 = $x1 / 180 * $pi;
        $w2 = $y2 / 180 * $pi;
        $j2 = $x2 / 180 * $pi;
        if ($j1 == $j2) {
            if ($w1 > $w2)
                return 270; // 北半球的情况，南半球忽略
            else if ($w1 < $w2)
                return 90;
            else
                return -1;// 位置完全相同
        }
        $ret = 4* pow(sin(($w1 - $w2) / 2), 2)- pow(
                sin(($j1 - $j2) / 2) * (cos($w1) - cos($w2)),2);
        $ret = sqrt($ret);
        $temp = (sin(abs($j1 - $j2) / 2) * (cos($w1) + cos($w2)));
        $ret = $ret / $temp;
        $ret = atan($ret) / $pi * 180;
        if ($j1 > $j2){ // 1为参考点坐标
            if ($w1 > $w2)
                $ret += 180;
            else
                $ret = 180 - $ret;
        } else if ($w1 > $w2)
            $ret = 360 - $ret;
        return $ret;
    }
    public function getDirection($lat1, $lng1, $lat2,$lng2) {
        $jiaodu = self::getAngle($lat1, $lng1, $lat2, $lng2);
        if (($jiaodu <= 10) || ($jiaodu > 350))
            return "东";
        if (($jiaodu > 10) && ($jiaodu <= 80))
            return "东北";
        if (($jiaodu > 80) && ($jiaodu <= 100))
            return "北";
        if (($jiaodu > 100) && ($jiaodu <= 170))
            return "西北";
        if (($jiaodu > 170) && ($jiaodu <= 190))
            return "西";
        if (($jiaodu > 190) && ($jiaodu <= 260))
            return "西南";
        if (($jiaodu > 260) && ($jiaodu <= 280))
            return "南";
        if (($jiaodu > 280) && ($jiaodu <= 350))
            return "东南";
        return "";
    }

    //二维数组排序
    function sortArr($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
        $key_arrays =array();
        if(is_array($arrays)){
            foreach ($arrays as $array){
                if(is_array($array)){
                    $key_arrays[] = $array[$sort_key];
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
        array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
        return $arrays;
    }
    //二维数组多键值排序
    function my_sort($arrays,$sort_key,$sort_key1,$sort_order=SORT_ASC,$sort_order1=SORT_ASC){
        $key_arrays = $key_arrays1 = array();
        if(is_array($arrays)){
            foreach ($arrays as $array){
                if(is_array($array)){
                    $key_arrays[] = $array[$sort_key];
                    $key_arrays1[] = $array[$sort_key1];
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
        array_multisort($key_arrays,$sort_order,$key_arrays1,$sort_order1,$arrays);
        return $arrays;
    }
    function azimuth4($Angle){

        $ssss=array();
        $s1=$s2=$s3=$s4=0;
        $e1=$e2=$e3=$e4=0;
        $z1=$z2=$z3=$z4=0;
        //$Angle=170;
        if($Angle+180>=360){
            $se1=$Angle-180;
            if($se1>=45){
                $s1=$se1-45;
            }else{
                $s1=360-(45-$se1);
            }
            $e1=$se1+45;
            $z1=$Angle-180;
        }else{
            $se1=$Angle+180;
            if($se1+45>=360){
                $e1=($se1+45)-360;
            }else{
                $e1=$se1+45;
            }
            $s1=$se1-45;
            $z1=$Angle+180;
        }
        $ssss[1]=[$s1,$z1,$e1];

        if($Angle+90>=360){
            $se2=360-($Angle+90);
            if($Angle+45>=360){
                $s2=($Angle+45)-360;
            }else{
                $s2=$Angle+45;
            }
            if($s2+90>=360){
                $e2=($s2+90)-360;
            }else{
                $e2=$s2+90;
            }
            $z2=($Angle+90)-360;
        }else{
            $s2=$Angle+45;
            $se2=($Angle+90);
            if($se2+45>=360){
                $e2=($se2+45)-360;
            }else{
                $e2=$se2+45;
            }
            $z2=($Angle+90);
        }
        $ssss[2]=[$s2,$z2,$e2];

        if($Angle>=135){
            $se3=($Angle-90);
            $s3=$se3-45;
            if($se3+45>=360){
                $e3=($se3+45)-360;
            }else{
                $e3=$se3+45;
            }
            $z3=$se3;
        }else{
            if($Angle-90>=0){
                $se3=($Angle-90);
                $e3=$se3+45;
                if($se3-45>=0){
                    $s3=$se3-45;
                }else{
                    $s3=360+($se3-45);
                }
                $z3=$se3;
            }else{
                $se3=360+($Angle-90);
                $s3=$se3-45;
                if($se3+45>=360){
                    $e3=($se3+45)-360;
                }else{
                    $e3=($se3+45);
                }
                $z3=$se3;
            }
        }
        $ssss[3]=[$s3,$z3,$e3];

        $z4=$Angle;
        if($Angle-45>=0){
            $s4=$Angle-45;
        }else{
            $s4=360-($Angle-45);
        }
        if($Angle+45>=360){
            $e4=($Angle+45)-360;
        }else{
            $e4=($Angle+45);
        }
        $ssss[4]=[$s4,$z4,$e4];
        return $ssss;
    }

    function rad($d){
        $Pi = pi();
        return $d*$Pi/180.0;
    }

    /* 根据经纬度、方位角、距离，求目标经纬度
    * @param $STARTLAT 纬度
    * @param $STARTLONG 经度
    * @param $ANGLE1 方位角
    * @param $DISTANCE 距离（米）
    */
    function computationx($STARTLAT, $STARTLONG, $ANGLE1, $DISTANCE){
        $Pi = pi();
        // return $Pi;
        $B1 = $STARTLAT;
        $L1 = $STARTLONG;
        $A1 = $ANGLE1;
        $S = $DISTANCE;
        $a = 6378245.0;
        $b = 6356752.3142;
        $c = $a*$a/$b;
        $alpha = ($a - $b) /$a;
        $e = sqrt($a *$a - $b *$b) /$a;
        $e2 = sqrt($a *$a - $b *$b) / $b;
        $B1 = $this->rad($B1);
        $L1 = $this->rad($L1);
        $A1 = $this->rad($A1);
        $W = sqrt(1 - $e *$e * (sin($B1) *sin($B1)));
        $V = $W * ($a / $b);
        //Dim W1 As Double
        $E1 = $e ;//第一偏心率
        //计算起点的归化纬度
        $W1 = $W;
        //Sqr(1 - e1 * e1 * Sin(B1 ) * Sin(B1 ))
        $sinu1 = sin($B1) * sqrt(1 - $E1 * $E1) / $W1;
        $cosu1 = cos($B1) / $W1;
        //计算辅助函数值
        $sinA0 = $cosu1 * sin($A1);
        $cotq1 = $cosu1 * cos($A1);
        $sin2q1 = 2 * $cotq1 / ($cotq1*$cotq1 + 1);
        $cos2q1 = ($cotq1 *$cotq1 - 1) / ($cotq1*$cotq1 + 1);
        //计算系数AA,BB,CC及AAlpha, BBeta的值。
        $cos2A0 = 1 - $sinA0 *$sinA0;
        $e2 = sqrt($a *$a - $b *$b) / $b;
        $k2 = $e2 * $e2 * $cos2A0;
        //Dim aa, BB, CC, EE22, AAlpha, BBeta As Double
        $aa = $b * (1 + $k2 / 4 - 3 * $k2 * $k2 / 64 + 5 * $k2 * $k2 * $k2 / 256);
        $BB = $b * ($k2 / 8 - $k2 * $k2 / 32 + 15 * $k2 * $k2 * $k2 / 1024);
        $CC = $b * ($k2 * $k2 / 128 - 3 * $k2 * $k2 * $k2 / 512);
        $e2 = $E1 * $E1;
        $AAlpha = ($e2 / 2 + $e2 * $e2 / 8 + $e2 * $e2 * $e2 / 16) - ($e2 * $e2 / 16 + $e2 * $e2 * $e2 / 16) * $cos2A0 + (3 * $e2 * $e2* $e2 / 128) * $cos2A0 * $cos2A0;
        $BBeta = ($e2 * $e2 / 32 + $e2 * $e2 * $e2 / 32) * $cos2A0 - ($e2 * $e2 * $e2 / 64) * $cos2A0 * $cos2A0;
        //计算球面长度
        $q0 = ($S - ($BB + $CC * $cos2q1) * $sin2q1) / $aa;
        $sin2q1q0 = $sin2q1 * cos(2 * $q0) + $cos2q1 * sin(2 * $q0);
        $cos2q1q0 = $cos2q1 * cos(2 * $q0) - $sin2q1 * sin(2 * $q0);
        $q = $q0 + ($BB + 5 * $CC * $cos2q1q0) * $sin2q1q0 / $aa;
        //'// 计算经度差改正数
        $theta = ($AAlpha * $q + $BBeta * ($sin2q1q0 - $sin2q1)) * $sinA0;
        //计算终点大地坐标及大地方位角
        $sinu2 = $sinu1 * cos($q) + $cosu1 * cos($A1) * sin($q);
        $B2 = atan($sinu2 / (sqrt(1 - $E1 * $E1) * sqrt(1 - $sinu2 * $sinu2))) * 180 / $Pi;
        $lamuda = atan(sin($A1) * sin($q) / ($cosu1 * cos($q) - $sinu1 * sin($q) * cos($A1))) * 180 / $Pi;
        if (sin($A1) > 0){
            if (sin($A1) * sin($q) / ($cosu1 * cos($q) - $sinu1 * sin($q) * cos($A1)) > 0){
                $lamuda = abs($lamuda);
            }else{
                $lamuda = 180 - abs($lamuda);}
        }else{
            if (sin($A1) * sin($q) / ($cosu1 * cos($q) - $sinu1 * sin($q) * cos($A1)) > 0){
                $lamuda = abs($lamuda) - 180;}
            else{
                $lamuda = -abs($lamuda);}
        }
        $L2 = $L1 * 180 / $Pi + $lamuda - $theta * 180 / $Pi;
        $A2 = atan($cosu1 * sin($A1) / ($cosu1 * cos($q) * cos($A1) - $sinu1 * sin($q))) * 180 / $Pi;
        if (sin($A1) > 0){
            if ($cosu1 * sin($A1) / ($cosu1 * cos($q) * cos($A1) - $sinu1 * sin($q)) > 0){
                $A2 = 180 + abs($A2);}else{
                $A2 = 360 - abs($A2);}
        }else{
            if ($cosu1 * sin($A1) / ($cosu1 * cos($q) * cos($A1) - $sinu1 * sin($q)) > 0){
                $A2 = abs($A2);
            }else{
                $A2 = 180 - abs($A2);
            }
        }//A2代表北偏西的角度
        $L2=round($L2,6);
        $B2=round($B2,6);
        $location=array("lng"=>$L2,"lat"=>$B2);
        return $location;//返回坐标数组
    }

    //随机姓名
    public function generateName(){
        $arrXing = $this->getXingList();
        $numbXing = count($arrXing);
        $arrMing = $this->getMingList();
        $numbMing =  count($arrMing);

        $Xing = $arrXing[mt_rand(0,$numbXing-1)];
        $Ming = $arrMing[mt_rand(0,$numbMing-1)];
        $sj=rand(1,100);
        if($sj>=35&&$sj<=75){
            $Ming .= $arrMing[mt_rand(0,$numbMing-1)];
        }
        $name = $Xing.$Ming;

        return $name;

    }
    //获取姓氏
    public function getXingList(){
        $arrXing=array('赵','钱','孙','李','周','吴','郑','王','冯','陈','褚','卫','蒋','沈','韩','杨','朱','秦','尤','许','何','吕','施','张','孔','曹','严','华','金','魏','陶','姜','戚','谢','邹',
            '喻','柏','水','窦','章','云','苏','潘','葛','奚','范','彭','郎','鲁','韦','昌','马','苗','凤','花','方','任','袁','柳','鲍','史','唐','费','薛','雷','贺','倪','汤','滕','殷','罗',
            '毕','郝','安','常','傅','卞','齐','元','顾','孟','平','黄','穆','萧','尹','姚','邵','湛','汪','祁','毛','狄','米','伏','成','戴','谈','宋','茅','庞','熊','纪','舒','屈','项','祝',
            '董','梁','杜','阮','蓝','闵','季','贾','路','娄','江','童','颜','郭','梅','盛','林','钟','徐','邱','骆','高','夏','蔡','田','樊','胡','凌','霍','虞','万','支','柯','管','卢','莫',
            '柯','房','裘','缪','解','应','宗','丁','宣','邓','单','杭','洪','包','诸','左','石','崔','吉','龚','程','嵇','邢','裴','陆','荣','翁','荀','于','惠','甄','曲','封','储','仲','伊',
            '宁','仇','甘','武','符','刘','景','詹','龙','叶','幸','司','黎','溥','印','怀','蒲','邰','从','索','赖','卓','屠','池','乔','胥','闻','莘','党','翟','谭','贡','劳','逄','姬','申',
            '扶','堵','冉','宰','雍','桑','寿','通','燕','浦','尚','农','温','别','庄','晏','柴','瞿','阎','连','习','容','向','古','易','廖','庾','终','步','都','耿','满','弘','匡','国','文',
            '寇','广','禄','阙','东','欧','利','师','巩','聂','关','荆'/*,'司马','上官','欧阳','夏侯','诸葛','闻人','东方','赫连','皇甫','尉迟','公羊','澹台','公冶','宗政','濮阳','淳于','单于','太叔',
            '申屠','公孙','仲孙','轩辕','令狐','徐离','宇文','长孙','慕容','司徒','司空'*/);
        return $arrXing;

    }
    //获取名字
    public function getMingList(){
        $arrMing=array('伟','刚','勇','毅','俊','峰','强','军','平','保','东','文','辉','力','明','永','健','世','广','志','义','兴','良','海','山','仁','波','宁','贵','福','生','龙','元','全'
        ,'国','胜','学','祥','才','发','武','新','利','清','飞','彬','富','顺','信','子','杰','涛','昌','成','康','星','光','天','达','安','岩','中','茂','进','林','有','坚','和','彪','博','诚'
        ,'先','敬','震','振','壮','会','思','群','豪','心','邦','承','乐','绍','功','松','善','厚','庆','磊','民','友','裕','河','哲','江','超','浩','亮','政','谦','亨','奇','固','之','轮','翰'
        ,'朗','伯','宏','言','若','鸣','朋','斌','梁','栋','维','启','克','伦','翔','旭','鹏','泽','晨','辰','士','以','建','家','致','树','炎','德','行','时','泰','盛','雄','琛','钧','冠','策'
        ,'腾','楠','榕','风','航','弘','秀','娟','英','华','慧','巧','美','娜','静','淑','惠','珠','翠','雅','芝','玉','萍','红','娥','玲','芬','芳','燕','彩','春','菊','兰','凤','洁','梅','琳'
        ,'素','云','莲','真','环','雪','荣','爱','妹','霞','香','月','莺','媛','艳','瑞','凡','佳','嘉','琼','勤','珍','贞','莉','桂','娣','叶','璧','璐','娅','琦','晶','妍','茜','秋','珊','莎'
        ,'锦','黛','青','倩','婷','姣','婉','娴','瑾','颖','露','瑶','怡','婵','雁','蓓','纨','仪','荷','丹','蓉','眉','君','琴','蕊','薇','菁','梦','岚','苑','婕','馨','瑗','琰','韵','融','园'
        ,'艺','咏','卿','聪','澜','纯','毓','悦','昭','冰','爽','琬','茗','羽','希','欣','飘','育','滢','馥','筠','柔','竹','霭','凝','晓','欢','霄','枫','芸','菲','寒','伊','亚','宜','可','姬'
        ,'舒','影','荔','枝','丽','阳','妮','宝','贝','初','程','梵','罡','恒','鸿','桦','骅','剑','娇','纪','宽','苛','灵','玛','媚','琪','晴','容','睿','烁','堂','唯','威','韦','雯','苇','萱'
        ,'阅','彦','宇','雨','洋','忠','宗','曼','紫','逸','贤','蝶','菡','绿','蓝','儿','翠','烟');
        return $arrMing;
    }

    //随机手机号
    public function SJtel(){
        $arr = array(
            130,131,132,133,134,135,136,137,138,139,
            144,147,
            150,151,152,153,155,156,157,158,159,
            176,177,178,
            180,181,182,183,184,185,186,187,188,189,
        );
        return $arr[array_rand($arr)].mt_rand(1000,9999).mt_rand(1000,9999);
    }


    /**
     * 判断一个坐标是否在一个多边形内（由多个坐标围成的）
     * 基本思想是利用射线法，计算射线与多边形各边的交点，如果是偶数，则点在多边形外，否则
     * 在多边形内。还会考虑一些特殊情况，如点在多边形顶点上，点在多边形边上等特殊情况。
     * @param $point 指定点坐标
     * @param $pts 多边形坐标 顺时针方向
     */
    function is_point_in_polygon($point, $pts) {
        $N = count($pts);
        $boundOrVertex = true; //如果点位于多边形的顶点或边上，也算做点在多边形内，直接返回true
        $intersectCount = 0;//cross points count of x
        $precision = 2e-10; //浮点类型计算时候与0比较时候的容差
        $p1 = 0;//neighbour bound vertices
        $p2 = 0;
        $p = $point; //指定坐标点

        $p1 = $pts[0];//left vertex
        for ($i = 1; $i <= $N; ++$i) {//check all rays
            // dump($p1);
            if ($p['lng'] == $p1['lng'] && $p['lat'] == $p1['lat']) {
                return $boundOrVertex;//p is an vertex
            }

            $p2 = $pts[$i % $N];//right vertex
            if ($p['lat'] < min($p1['lat'], $p2['lat']) || $p['lat'] > max($p1['lat'], $p2['lat'])) {//ray is outside of our interests
                $p1 = $p2;
                continue;//next ray left point
            }

            if ($p['lat'] > min($p1['lat'], $p2['lat']) && $p['lat'] < max($p1['lat'], $p2['lat'])) {//ray is crossing over by the algorithm (common part of)
                if($p['lng'] <= max($p1['lng'], $p2['lng'])){//x is before of ray
                    if ($p1['lat'] == $p2['lat'] && $p['lng'] >= min($p1['lng'], $p2['lng'])) {//overlies on a horizontal ray
                        return $boundOrVertex;
                    }

                    if ($p1['lng'] == $p2['lng']) {//ray is vertical
                        if ($p1['lng'] == $p['lng']) {//overlies on a vertical ray
                            return $boundOrVertex;
                        } else {//before ray
                            ++$intersectCount;
                        }
                    } else {//cross point on the left side
                        $xinters = ($p['lat'] - $p1['lat']) * ($p2['lng'] - $p1['lng']) / ($p2['lat'] - $p1['lat']) + $p1['lng'];//cross point of lng
                        if (abs($p['lng'] - $xinters) < $precision) {//overlies on a ray
                            return $boundOrVertex;
                        }

                        if ($p['lng'] < $xinters) {//before ray
                            ++$intersectCount;
                        }
                    }
                }
            } else {//special case when ray is crossing through the vertex
                if ($p['lat'] == $p2['lat'] && $p['lng'] <= $p2['lng']) {//p crossing over p2
                    $p3 = $pts[($i+1) % $N]; //next vertex
                    if ($p['lat'] >= min($p1['lat'], $p3['lat']) && $p['lat'] <= max($p1['lat'], $p3['lat'])) { //p.lat lies between p1.lat & p3.lat
                        ++$intersectCount;
                    } else {
                        $intersectCount += 2;
                    }
                }
            }
            $p1 = $p2;//next ray left point
        }

        if ($intersectCount % 2 == 0) {//偶数在多边形外
            return false;
        } else { //奇数在多边形内
            return true;
        }
    }
    /**
     * 通过URL获取页面信息
     * @param $url 地址
     * @return mixed 返回页面信息
     */
    function file_get_content($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url); //设置访问的url地址
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
        $result = curl_exec($ch);
        curl_close ($ch);
        return $result;
    }

public function sendMes()
    {
        Tools::sendMes1();

    }
}
